/**
 * 首页与核算进度达成图表组件
 */
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'
import http from '@/axios/http'
import adaptivePx from '@/utils/adaptivePx'

const main = ref()
const progressChart = ref()
let myChart = null

const date = new Date()
const lastYear = (date.getFullYear() - 1) + '年'
const thisYear = date.getFullYear() + '年'

async function getProgressChart() {
  await http.post('/home/getProgressChart')
  .then(res => {
    progressChart.value = res.data
  }).catch(err => {
    console.log(err)
  })
}

function init() {
  myChart = echarts.init(main.value, null, { renderer: 'svg'})

  let option = {
    title: {
      text: '与核算进度达成⭐',
      textStyle: {
        color: 'white',
        fontSize: adaptivePx(16),
        fontWeight: 'normal'
      },
      left: '2%',
      top: '2%'
    },
    color: ['#1e90ff', '#2fa8c3'],
    legend: {
      textStyle: {
        color: 'white',
        fontSize: adaptivePx(14),
        fontWeight: 'normal'
      },
      top: '2%',
      right: '15%',
      data: [lastYear, thisYear]
    },
    grid: {
      left: '3%',
      top: '27%',
      right: '5%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      boundaryGap: false,
      axisLabel: {
        color: 'white',
        fontSize: adaptivePx(12),
        fontWeight: 'normal'
      },
      data: progressChart.value.month
    },
    yAxis: {
      max: 120,
      min: 40,
      interval: 20,
      axisLabel: {
        color: 'white',
        formatter: '{value} %',
        fontSize: adaptivePx(12),
        fontWeight: 'normal'
      },
      axisLine: {
        lineStyle: {
          type: 'solid'
        },
        show: true
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: '#666'
        },
        show: true
      }
    },
    series: [
      {
        name: lastYear,
        type: 'line',
        data: progressChart.value.lastProgressChart,
        emphasis: {
          scale: 3
        },
        markPoint: {
          symbol: progressChart.value.lastProgressChart[progressChart.value.lastProgressChart.length - 1] > 0 ? 'pin' : 'none',
          symbolSize: adaptivePx(55),
          symbolRotate: progressChart.value.lastProgressChart[progressChart.value.lastProgressChart.length - 1] < progressChart.value.thisProgressChart[12] ? 180 : 0,
          label: {
            offset: [0, progressChart.value.lastProgressChart[progressChart.value.lastProgressChart.length - 1] < progressChart.value.thisProgressChart[12] ? adaptivePx(10) : 0],
            color: 'white',
            fontSize: adaptivePx(12),
            fontWeight: 'normal'
          },
          data: [
            {
              name: 'Total',
              coord: [12, progressChart.value.lastProgressChart[progressChart.value.thisProgressChart.length - 1]],
              value: progressChart.value.lastProgressChart[progressChart.value.thisProgressChart.length - 1] + '%'
            }
          ]
        }
      },
      {
        name: thisYear,
        type: 'line',
        data: progressChart.value.thisProgressChart,
        areaStyle: {
          opacity: 0.2
        },
        emphasis: {
          scale: 3
        },
        markPoint: {
          symbol: progressChart.value.thisProgressChart[progressChart.value.thisProgressChart.length - 1] > 0 ? 'pin' : 'none',
          symbolSize: adaptivePx(55),
          symbolRotate: progressChart.value.thisProgressChart[progressChart.value.thisProgressChart.length - 1] < progressChart.value.lastProgressChart[progressChart.value.lastProgressChart.length - 1] ? 180 : 0,
          label: {
            offset: [0, progressChart.value.thisProgressChart[progressChart.value.thisProgressChart.length - 1] < progressChart.value.lastProgressChart[progressChart.value.lastProgressChart.length - 1] ? adaptivePx(10) : 0],
            color: 'white',
            fontSize: adaptivePx(12),
            fontWeight: 'normal'
          },
          data: [
            {
              name: 'Total',
              coord: [progressChart.value.thisProgressChart.length - 1, progressChart.value.thisProgressChart[progressChart.value.thisProgressChart.length - 1]],
              value: progressChart.value.thisProgressChart[progressChart.value.thisProgressChart.length - 1] + '%'
            }
          ]
        }
      }
    ],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'line',
        axis: 'x',
        lineStyle: {
          type: 'solid',
          color: '#ccc',
          width: 3
        }
      },
      textStyle: {
        color: 'black',
        fontSize: adaptivePx(14),
        fontWeight: 'normal'
      },
      backgroundColor: 'rgba(171, 182, 212, 0.9)',
      borderWidth: 0,
      formatter: function (params) {
        let content = ''
        if (params[0]) {
          content += `
            ${ params[0].axisValue }<br>
            <div style="display: inline-block; width: 10px; height: 10px; border-radius: 5px; background-color: ${ params[0].seriesName === lastYear ? '#1e90ff' : '#2fa8c3' };"></div>&nbsp;
            ${ params[0].seriesName === lastYear ? '去年' : '今年' }&emsp;${ params[0].value ? params[0].value + '%' : '-' }&nbsp;&nbsp;
          `
        }
        
        if (params[0] && params[1] && params[0].value && params[1].value) {
          let diff = params[0].value - params[1].value
          if (diff > 0) {
            content += `
            <span style="color: blue;">+${ diff }%</span>
            `
          }
          if (diff < 0) {
            content += `
            <span style="color: red;">${ diff }%</span>
            `
          }
        }
        
        if (params[1]) {
          content += `
            <br>
            <div style="display: inline-block; width: 10px; height: 10px; border-radius: 5px; background-color: ${ params[1].seriesName === lastYear ? '#1e90ff' : '#2fa8c3' };"></div>&nbsp;
            ${ params[1].seriesName === lastYear ? '去年' : '今年' }&emsp;${ params[1].value ? params[1].value + '%' : '-' }
          `
        }
        
        return content
      }
    }
  }

  myChart.setOption(option)

  myChart.on('legendselectchanged', () => {
    event.stopPropagation()
  })
}

function myChartResize() {
  if (myChart) {
    myChart.resize()
  }
}

onMounted(async () => {
  await getProgressChart()
  init()
  
  window.addEventListener('resize', myChartResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', myChartResize)
})
</script>

<template>
  <div id="home-progress-chart" ref="main"></div>
</template>

<style lang="scss" scoped>
#home-progress-chart {
  width: 100%;
  height: 100%;
}
</style>